﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ElectricBillMaker.Properties;

namespace ElectricBillMaker
{
    interface IView
    {
        int GetPrevCounter1();
        int GetPrevCounter2();
        int GetPrevCounter3();

        int GetCurCounter1();
        int GetCurCounter2();
        int GetCurCounter3();

        int GetDelta1();
        int GetDelta2();
        int GetDelta3();

        float GetTarif1();
        float GetTarif2();
        float GetTarif3();

        int GetPrivilegeKw();
        int GetPrivilegePercent();

        string GetBook();
        string GetAbonent();
        string GetKr();
        string GetName();
        string GetAddress();
        
        int GetMonth();
        int GetYear();
        string GetMonthString();
        string GetYearString();

        void OnDeltaChanged(int delta1, int delta2, int delta3);
    }


    public partial class Form1 : Form, IModelChangeEvents, IView
    {
        private Model model_;
        private Controller controller_;

        public Form1()
        {
            model_ = new Model(this, new Database(@"db.sdf")); 
            controller_ = new Controller(model_, this);

            InitializeComponent();
        }

        private void calculate_Click(object sender, EventArgs e)
        {
            controller_.OnCalculate();
        }

        public void OnCalculated(float toPay)
        {
            payTotal.Text = toPay.ToString();
        }

        public int GetPrevCounter1()
        {
            return Convert.ToInt32(prev1.Value);
        }

        public int GetPrevCounter2()
        {
            return Convert.ToInt32(prev2.Value);
        }

        public int GetPrevCounter3()
        {
            return Convert.ToInt32(prev3.Value);
        }

        public int GetCurCounter1()
        {
            return Convert.ToInt32(current1.Value);
        }

        public int GetCurCounter2()
        {
            return Convert.ToInt32(current2.Value);
        }

        public int GetCurCounter3()
        {
            return Convert.ToInt32(current3.Value);
        }

        public int GetDelta1()
        {
            return Convert.ToInt32(diff1.Text);
        }

        public int GetDelta2()
        {
            return Convert.ToInt32(diff2.Text);
        }

        public int GetDelta3()
        {
            return Convert.ToInt32(diff3.Text);
        }

        public float GetTarif1()
        {
            return Convert.ToSingle(tariff1.Text);
        }

        public float GetTarif2()
        {
            return Convert.ToSingle(tariff2.Text);
        }

        public float GetTarif3()
        {
            return Convert.ToSingle(tariff3.Text);
        }

        public void OnDeltaChanged(int delta1, int delta2, int delta3)
        {
            diff1.Text = delta1.ToString();
            diff2.Text = delta2.ToString();
            diff3.Text = delta3.ToString();
        }

        public int GetPrivilegeKw()
        {
            return Convert.ToInt32(privilegeKw.Value);
        }  

        public int GetPrivilegePercent()
        {
            return Convert.ToInt32(privilegePercent.Value);
        }

        public void OnToPay1Change(float toPay1)
        {
            pay1.Text = toPay1.ToString();
        }

        public void OnToPay2Change(float toPay2)
        {
            pay2.Text = toPay2.ToString();
        }

        public void OnToPay3Change(float toPay3)
        {
            pay3.Text = toPay3.ToString();
        }

        public string GetBook()
        {
            return book.Text;
        }

        public string GetAbonent()
        {
            return abonent.Text;
        }

        public string GetKr()
        {
            return kr.Text;
        }

        public string GetName()
        {
            return name.Text;
        }
        
        public string GetAddress()
        {
            return address.Text;
        }

        public int GetMonth()
        {
            return comboBoxMonth.SelectedIndex + 1;
        }

        public string GetMonthString()
        {
            return comboBoxMonth.SelectedItem.ToString();
        }


        public int GetYear()
        {
            return Convert.ToInt32(numericUpDownYear.Value);
        }
        
        public string GetYearString()
        {
            return GetYear().ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadSettings();

            this.numericUpDownYear.ValueChanged += new System.EventHandler(this.numericUpDownYear_ValueChanged);
            this.comboBoxMonth.SelectedIndexChanged += new System.EventHandler(this.comboBoxMonth_SelectedIndexChanged);

            //
            // TODO: remove this ugly crutch. Move all initialization to Model.
            //

            controller_.OnMonthChange();
        }

        private void LoadSettings()
        {
            UpgradeSettings();

            name.Text = Settings.Default.Name;
            address.Text = Settings.Default.Address;
            abonent.Text = Settings.Default.Abonent;
            book.Text = Settings.Default.Book;
            kr.Text = Settings.Default.Kr;
            prev1.Value = Settings.Default.Prev1;
            prev2.Value = Settings.Default.Prev2;
            prev3.Value = Settings.Default.Prev3;
            current1.Value = Settings.Default.Cur1;
            current2.Value = Settings.Default.Cur2;
            current3.Value = Settings.Default.Cur3;
            if (Settings.Default.Tarif1 != "")
            {
                tariff1.Text = Settings.Default.Tarif1;
            }
            if (Settings.Default.Tarif2 != "")
            {
                tariff2.Text = Settings.Default.Tarif2;
            }
            if (Settings.Default.Tarif3 != "")
            {
                tariff3.Text = Settings.Default.Tarif3;
            }
            privilegeKw.Value = Settings.Default.PrivilegeKw;
            privilegePercent.Value = Settings.Default.PrivilegePercent;
            DateTime month = Settings.Default.Month;
            numericUpDownYear.Value = month.Year;
            comboBoxMonth.SelectedIndex = month.Month - 1;
        }

        private void UpgradeSettings()
        {
            if (Settings.Default.Upgrade)
            {
                Settings.Default.Upgrade();
                Settings.Default.Upgrade = false;
                Settings.Default.Save();
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            SaveSettings();
        }

        private void SaveSettings()
        {
            Settings.Default.Name = name.Text;
            Settings.Default.Address = address.Text;
            Settings.Default.Abonent = abonent.Text;
            Settings.Default.Book = book.Text;
            Settings.Default.Kr = kr.Text;
            Settings.Default.Prev1 = Convert.ToInt32(prev1.Value);
            Settings.Default.Prev2 = Convert.ToInt32(prev2.Value);
            Settings.Default.Prev3 = Convert.ToInt32(prev3.Value);
            Settings.Default.Cur1 = Convert.ToInt32(current1.Value);
            Settings.Default.Cur2 = Convert.ToInt32(current2.Value);
            Settings.Default.Cur3 = Convert.ToInt32(current3.Value);
            Settings.Default.Tarif1 = tariff1.Text;
            Settings.Default.Tarif2 = tariff2.Text;
            Settings.Default.Tarif3 = tariff3.Text;
            Settings.Default.PrivilegeKw = Convert.ToInt32(privilegeKw.Value);
            Settings.Default.PrivilegePercent = Convert.ToInt32(privilegePercent.Value);
            Settings.Default.Month = new DateTime((int)numericUpDownYear.Value // year
                                , comboBoxMonth.SelectedIndex + 1 // month
                                , 1);

            Settings.Default.Save();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            controller_.OnCalculate();
            controller_.OnBill();
        }

        private void comboBoxMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            controller_.OnMonthChange();
        }

        private void numericUpDownYear_ValueChanged(object sender, EventArgs e)
        {
            controller_.OnYearChange();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            controller_.OnNewCalc();
        }
    }
}
